package mysql

import (
	"gorm.io/gorm"
	"onerk4/zg5yk/ordering/common/global"
	"onerk4/zg5yk/ordering/common/model"
)

func QueryUserInfoByMobile(mobile string) (result *model.User, err error) {
	err = global.DB.Model(&model.User{}).Where("mobile = ?", mobile).Limit(1).Find(&result).Error
	return
}

func QueryCoupon(id int64) (result *model.Coupon, err error) {
	err = global.DB.Model(&model.Coupon{}).Where("id = ?", id).Limit(1).Find(&result).Error
	return
}

func QueryUserCoupon(id int64, uid int64) (result *model.UserCoupon, err error) {
	err = global.DB.Model(&model.UserCoupon{}).Where("uid = ? and coupon_id = ?", uid, id).Limit(1).Find(&result).Error
	return
}

func ReceiveCoupon(m *model.UserCoupon) error {
	return global.DB.Model(&model.UserCoupon{}).Create(&m).Error
}

func QueryShowCouponList(uid int64, status int64) (results []*model.UserCoupon, err error) {
	err = global.DB.Model(&model.UserCoupon{}).Where("uid = ? and status = ?", uid, status).Find(&results).Error
	return
}

func UpdateUserCoupon(uid, cid int64, sta int) error {
	return global.DB.Model(&model.UserCoupon{}).Where("id = ? and uid = ?", cid, uid).Limit(1).Update("status", sta).Error
}

func QueryUserInfoByUserId(uid int) (result *model.User, err error) {
	err = global.DB.Model(&model.User{}).Where("id = ?", uid).Limit(1).Find(&result).Error
	return
}

func UpdatePoints(uid int, points float64, tx *gorm.DB) error {
	return tx.Model(&model.User{}).Where("id = ?", uid).Limit(1).Update("points", points).Error
}
